import json

from django.core.paginator import Paginator
from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views import View
from haystack.views import SearchView

from apps.contents.models import ContentCategory
from apps.goods.models import GoodsChannel, GoodsCategory, SKU
from utils import models
from utils.goodsutils import get_categories, get_breadcrumb,get_goods_specs



class IndexView(View):

    def get(self, request):
        try:
            categories = get_categories()
        except Exception as e:
            print(e)
            return {'code': 1, 'errmsg': 'get data error'}

        # 获取广告数据
        contents = {}

        # 1 ContentCategory获取所有广告的类别
        content_categories = ContentCategory.objects.all()
        # 2 遍历所有类别 获取每个类别下的广告
        for content_cat in content_categories:
            content_items = content_cat.content_set.filter(status=True).order_by('sequence')
            # 获取的时候按照status 过滤
            # 按照sequence排序

            content_items_list = []
            for item_c in content_items:
                content_items_list.append(item_c.to_dict())

            contents[content_cat.key] = content_items_list
        print('contents', contents)

        return JsonResponse({'code': 0, 'errmsg': 'ok', "categories": categories, 'contents': contents})

        # return render(request,'index2.html',context)


# 商品列表页
class ListView(View):
    def get(self, request, category_id):
        # 接收参数
        page = request.GET.get('page')
        page_size = request.GET.get('page_size')
        ordering = request.GET.get('ordering')
        # 获取分类商品数据
        # 获取列表对象
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except Exception as e:
            print(e)
            return JsonResponse({'code': 400, 'errmsg': 'category query error'})
        skus = SKU.objects.filter(category=category, is_launched=True).order_by(ordering)

        # 分页 排序
        paginator = Paginator(skus, 3)
        page_skus = paginator.page(page)
        # 总共有几页
        count = paginator.num_pages
        # 面包屑导航
        breadcrumb = get_breadcrumb(category)
        # 热销商品数据
        sku_list = []
        for sku in page_skus:
            sku_list.append({
                'id': sku.id,
                'name': sku.name,
                'price': sku.price,
                'default_image_url': sku.default_image.url if sku.default_image else '',
            })

        return JsonResponse({'code': 0, 'errmsg': 'ok', 'count': count, 'list': sku_list,
                             'breadcrumb': breadcrumb})


class HotGoodsView(View):
    """商品热销排行"""

    def get(self, request, category_id):
        """提供商品热销排行JSON数据"""
        # 根据销量倒序
        skus = SKU.objects.filter(category_id=category_id, is_launched=True).order_by('-sales')[:2]

        # 序列化
        hot_skus = []
        for sku in skus:
            hot_skus.append({
                'id': sku.id,
                'default_image_url': sku.default_image.url if sku.default_image else '',
                'name': sku.name,
                'price': sku.price
            })

        return JsonResponse({'code': 0, 'errmsg': 'OK', 'hot_skus': hot_skus})


class MySearchView(SearchView):
    '''重写SearchView类'''

    def create_response(self):
        # 获取搜索结果
        context = self.get_context()
        print(context)
        data_list = []

        page_count = context['page'].paginator.num_pages
        print("pagecount",page_count)
        print(context['page'].object_list)
        for sku in context['page'].object_list:
            data_list.append({
                'id': sku.object.id,
                'name': sku.object.name,
                'price': sku.object.price,
                'default_image_url': sku.object.default_image.url if sku.object.default_image else '',
                'searchkey': context.get('query'),
                'page_size': context['page'].paginator.num_pages,
                'count': context['page'].paginator.count
            })
        # 拼接参数, 返回
        return JsonResponse({'code': 0, 'errmsg': 'ok', 'data_list': data_list, 'page_count': page_count}, safe=False)


class DetailView(View):
    """商品详情页"""

    def get(self, request, sku_id):
        """提供商品详情页"""
        # 获取当前sku的信息
        try:
            sku = SKU.objects.get(id=sku_id)
        except SKU.DoesNotExist:
            return render(request, '404.html')

        # 查询商品频道分类
        categories = get_categories()
        # 查询面包屑导航
        breadcrumb = get_breadcrumb(sku.category)

        # 查询SKU规格信息
        goods_specs = get_goods_specs(sku)

        # 渲染页面
        context = {
            'categories':categories,
            'breadcrumb':breadcrumb,
            'sku':sku,
            'specs': goods_specs,
        }
        return render(request, 'detail.html', context)

class DetailVisitView(View):
    """详情页分类商品访问量"""

    def post(self, request, category_id):
         try:
             # 1.获取当前商品
             category = GoodsCategory.objects.get(id=category_id)
         except Exception as e:
             return JsonResponse({'code': 400, 'errmsg': '缺少必传参数'})

         # 2.查询日期数据
         from datetime import date
         # 获取当天日期
         today_date = date.today()

         from apps.goods.models import GoodsVisitCount
         try:
             # 3.如果有当天商品分类的数据  就累加数量
             count_data = category.goodsvisitcount_set.get(date=today_date)
         except:
             # 4. 没有, 就新建之后在增加
             count_data = GoodsVisitCount()

         try:
             count_data.count += 1
             count_data.category = category
             count_data.save()
         except Exception as e:
             return JsonResponse({'code': 400, 'errmsg': '新增失败'})

         return JsonResponse({'code': 0, 'errmsg': 'OK'})
